const root = {
  val: 2,
  left: null,
  right: {
    val: 3,
    left: null,
    right: {
      val: 4,
      left: null,
      right: {
        val: 5,
        left: null,
        right: {
          val: 6,
        }
      }
    }
  }
}

let nowHigh = 0;
let resArray = [];
var minDepth = function (root) {
  deep(root);
  return Math.min(...resArray);
};

let deep = function (root) {
  if (!root) return;
  nowHigh++;
  if (!root.left && !root.right) {
    resArray.push(nowHigh);
    return;
  }
  deep(root.left);
  deep(root.right);
  nowHigh--;
}

console.log(minDepth(root));